package tasks 
{
	import app.AppModel;
	import app.FurnitureClass;
	import app.modules.ChooseAttribModel;
	import app.modules.ChooseSectionInfo;
	import app.modules.NumStepperInfo;
	import app.modules.RadioButtonInfo;
	import app.modules.TargetProperty;
	import app.modules.TitleInfo;
	import app.modules.TreeFolderInfo;
	import gui.factory.StyleFactory;
	import gui.NumStepper;
	import gui.PositionInfo;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class LoadButtons extends BaseXmlLoadTask
	{
		public static const BOTTOM_FURNITURES:int = 0;
		private var _treeFolder:Vector.<TreeFolderInfo>;
		
		public function LoadButtons(xmlFile:String, treeFolder: Vector.<TreeFolderInfo>) 
		{
			super(xmlFile);
			_treeFolder = treeFolder;
		}
		
		override protected function onXMLReaded(xml:XML):void 
		{
			// chek loader type
			var folderList:XMLList;
			try
			{
				folderList = xml.folder;
				var count:int = folderList.length();
				for (var i:int = 0; i < count; i++)
				{
					// read folder
					var folderInfo:TreeFolderInfo = new TreeFolderInfo();
					folderInfo.title = folderList[i].attribute("title");
					folderInfo.header = folderList[i].attribute("header");
					// read nodes
					var nodesList:XMLList = folderList[i].node;
					readNodes(nodesList, folderInfo);
					_treeFolder.push(folderInfo);
					//addFolderToModel(folderInfo);
				}
			}
			catch(err:Error)
			{
				handleError("Error while XML: " + err.errorID + "; " + err.name + "; "  + err.message);
				complete(false);
				return;
			}
			complete(true);
		}
		
		/*private function addFolderToModel(folderInfo:TreeFolderInfo):void 
		{
			switch(_loaderType)
			{
				case BOTTOM_FURNITURES:
						_model.bottomFurnitures.push(folderInfo);
					break;
			}
		}*/
		
		private function readNodes(nodesList:XMLList, folderInfo:TreeFolderInfo):void 
		{
			try
			{
				var count:int = nodesList.length();
				for (var i:int = 0; i < count; i++)
				{
					// read sections
					var chooseAttribModel:ChooseAttribModel = new ChooseAttribModel();
					chooseAttribModel.titleRB = nodesList[i].attribute("title");
					var sectionsList:XMLList = nodesList[i].section;
					readSections(sectionsList, chooseAttribModel);
					folderInfo.addNode(chooseAttribModel);
				}
			}
			catch(err:Error)
			{
				handleError("Error read nodes: " + err.errorID + "; " + err.name + "; "  + err.message);
				return;
			}
			
		}
		
		/**
		 * Load section
		 * @param	sectionsList
		 * @param	chooseAttribModel
		 */
		private function readSections(sectionsList:XMLList, chooseAttribModel:ChooseAttribModel):void 
		{
			try
			{
				var count:int = sectionsList.length();
				for (var i:int = 0; i < count; i++)
				{
					// read attribs
					var section:ChooseSectionInfo = new ChooseSectionInfo();
					section.furnitureID = sectionsList[i].attribute("furniture");
					section.idPrefix = sectionsList[i].attribute("prefix");
					section.defWidth = sectionsList[i].attribute("defaultW");
					section.defHeight = sectionsList[i].attribute("defaultH");
					
					// readraio buttons
					readRadioButtons(sectionsList[i].elements("rb"), section);
					readNumericStepper(sectionsList[i].elements("numstepper"),section);
					var textList:XMLList = sectionsList[i].text;
					readText(textList, section);
					
					// read preview
					if (sectionsList[i].elements("preview").length() > 0)
					{
						section.preview = sectionsList[i].elements("preview")[0].attribute("source");
						section.previewPos = getPosition(sectionsList[i].elements("preview")[0]);
					}
					
					chooseAttribModel.addSection(section);
				}
			}
			catch(err:Error)
			{
				handleError("Error read sections: " + err.errorID + "; " + err.name + "; "  + err.message);
				return;
			}
		}
		
		private function readText(textList:XMLList, section:ChooseSectionInfo):void 
		{
			try
			{
				var count:int = textList.length();
				for (var i:int = 0; i < count; i++)
				{
					// read text
					
					var titleInfo:TitleInfo = new TitleInfo();
					titleInfo.text = textList[i].attribute("title");
					titleInfo.position = getPosition(textList[i]);
					titleInfo.textWidth = getNumberFromStr(textList[i].attribute("textw"),
										StyleFactory.CHOOSE_ATTRIB_MODULE_WIDTH, "Wrong text width in radioBtn", 
										true);
					section.addTitle(titleInfo);
				}
			}
			catch(err:Error)
			{
				handleError("Error read text: " + err.errorID + "; " + err.name + "; "  + err.message);
				return;
			}
		}
		
		private function readRadioButtons(radioButtonsList:XMLList, section:ChooseSectionInfo):void 
		{
			try
			{
				var count:int = radioButtonsList.length();
				for (var i:int = 0; i < count; i++)
				{
					// read buttons
					var rbType:String = getTargetPropertyType(radioButtonsList[i].attribute("type"));
					if (rbType.length<1)
					{
						handleError("Warning! Wrong radio button type: " + radioButtonsList[i].attribute("type"));
						continue;
					}
					var radBtn:RadioButtonInfo = new RadioButtonInfo(rbType, radioButtonsList[i].attribute("value"));
					radBtn.position = getPosition(radioButtonsList[i]);
					radBtn.title = radioButtonsList[i].attribute("title");
					radBtn.textWidth = getNumberFromStr(radioButtonsList[i].attribute("textw"),
										StyleFactory.CHOOSE_ATTRIB_MODULE_WIDTH, "Wrong text width in radioBtn", 
										true);
					section.addRadioButton(radBtn);
				}
			}
			catch(err:Error)
			{
				handleError("Error read radio buttons: " + err.errorID + "; " + err.name + "; "  + err.message);
				return;
			}
		}
		
		private function readNumericStepper(numStepperList:XMLList, section:ChooseSectionInfo):void 
		{
			try
			{
				var count:int = numStepperList.length();
				for (var i:int = 0; i < count; i++)
				{
					// read buttons
					var targetProperty:String = getTargetPropertyType(numStepperList[i].attribute("type"));
					if (targetProperty.length < 1)
					{
						handleError("Warning! Wrong numeric stepper type type: " + numStepperList[i].attribute("type"));
						continue;
					}
					var numStepper:NumStepperInfo = new NumStepperInfo(targetProperty);
					numStepper.position = getPosition(numStepperList[i]);
					numStepper.title = numStepperList[i].attribute("title");
					if (numStepperList[i].attribute("values").length() > 0)
					{
						// get attribs
						numStepper.values = getArrayValuesFormStr(numStepperList[i].attribute("values"));
					}else
					{
						numStepper.min = getNumberFromStr(numStepperList[i].attribute("min"), 0,
									"NumStepper min wrong. Section.furnitre ID: " +section.furnitureID);
						numStepper.max = getNumberFromStr(numStepperList[i].attribute("max"), 0,
									"NumStepper max wrong. Section.furnitre ID: " +section.furnitureID);
						numStepper.step = getNumberFromStr(numStepperList[i].attribute("step"), 0,
									"NumStepper step wrong. Section.furnitre ID: " +section.furnitureID);
					}			
					section.addNumericStepper(numStepper);
				}
			}
			catch(err:Error)
			{
				handleError("Error read numeric stepper: " + err.errorID + "; " + err.name + "; "  + err.message);
				return;
			}
		}
		
		private function getArrayValuesFormStr(str:String):Vector.<String>
		{
			// delemiter: comma, Values: numbers ~ 3.14, string ~ property, string with number ~ proerty3.14
			var valueRegExp:RegExp = /[\w\d\.]+/g;
			var arrMatch:Array = str.match(valueRegExp);
			var values:Vector.<String> = new Vector.<String>();
			for (var i:int = 0; i < arrMatch.length; i++)
			{
				values.push(arrMatch[i]);
			}
			return values;
		}
		
		private function getPosition(xmlNode:XML):PositionInfo
		{
			var pos:PositionInfo = new PositionInfo();
			try
			{
				pos.posX = getNumberFromStr(xmlNode.attribute("x"), 0, "wrong number position x", true);
				pos.posY = getNumberFromStr(xmlNode.attribute("y"), 0, "wrong number position y", true);
				pos.leftPadding = getNumberFromStr(xmlNode.attribute("leftPadding"), 
												0, "wrong number position leftPadding", true);
												
				pos.rightPadding = getNumberFromStr(xmlNode.attribute("rightPadding"), 
												0, "wrong number position rightPadding", true);
												
				pos.topPadding = getNumberFromStr(xmlNode.attribute("topPadding"), 
												0, "wrong number position topPadding", true);
												
				pos.bottomPadding = getNumberFromStr(xmlNode.attribute("bottomPadding"), 
												0, "wrong number position bottomPadding", true);
				//pos.align = getAlign(xmlNode.attribute("align"));																
				pos.align = getAlignXML(xmlNode);																
			}
			catch(err:Error)
			{
				handleError("Error read position info: " + err.errorID + "; " + err.name + "; "  + err.message);
				return null;
			}
			return pos;
		}
		
		private function getAlignXML(alignXml:XML):int 
		{
			var align:int = PositionInfo.ALIGN_NONE;
			if (alignXml.attribute("centrx").length() > 0)
			{
				align = align | PositionInfo.ALIGN_CENTR_X;
			}
			if (alignXml.attribute("centry").length() > 0)
			{
				align = align | PositionInfo.ALIGN_CENTR_Y;
			}
			if (alignXml.attribute("right").length() > 0)
			{
				align = align | PositionInfo.ALIGN_RIGHT;
			}
			if (alignXml.attribute("left").length() > 0)
			{
				align = align | PositionInfo.ALIGN_LEFT;
			}
			if (alignXml.attribute("top").length() > 0)
			{
				align = align | PositionInfo.ALIGN_TOP;
			}
			if (alignXml.attribute("bottom").length() > 0)
			{
				align = align | PositionInfo.ALIGN_BOTTOM;
			}
			return align;
		}
		
		private function getAlign(alignXml:String):int 
		{
			var align:int = PositionInfo.ALIGN_NONE;
			switch(alignXml)
			{
				case "top":
					align = PositionInfo.ALIGN_TOP;
					break;
				case "bottom":
					align = PositionInfo.ALIGN_BOTTOM;
					break;
				case "centrx":
					align = PositionInfo.ALIGN_CENTR_X;
					break;
				case "centry":
					align = PositionInfo.ALIGN_CENTR_Y;
					break;
				case "left":
					align = PositionInfo.ALIGN_LEFT;
					break;
				case "right":
					align = PositionInfo.ALIGN_RIGHT;
					break;
			}
			return align;
		}
		
		private function getTargetPropertyType(typeXML:String):String
		{
			var type:String = "";
			switch(typeXML)
			{
				case "width":
						type = TargetProperty.WIDTH;
					break;
				case "height":
						type = TargetProperty.HEIGHT;
					break;
				case "side":
						type = TargetProperty.SIDE;
					break;
				case "extra":
						type = TargetProperty.EXTRA;
					break;
				default:
					log("unknow target type: " + typeXML);
					break;
			}
			return type;
		}
		
		
		override protected function handleError(err:String):void 
		{
			super.handleError("Load buttons: " + err);
		}
		
		override public function destroy():void 
		{
			_treeFolder = null;
			super.destroy();
		}
		
	}

}